package Utils;

import android.text.TextUtils;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;


/**
 * string util
 * Created by zhangdong on 2017/11/13.
 */

public class StringUtil {

    /**
     * Don't let anyone instantiate this class
     */
    private StringUtil() {
    }

    public static boolean isNullOrEmpty(String input) {
        if (null == input || input.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否都不是空
     *
     * @param strings 字符串
     * @return true:都不是空  false:有空字符串
     */
    public static boolean isAllNotNullOrEmpty(String... strings) {
        if (strings == null) {
            return false;
        }
        for (String string : strings) {
            if (isNullOrEmpty(string)) {
                return false;
            }
        }
        return true;
    }

    public static String getFormatPrice(double price) {
        return String.format("%.2f", price);
    }

    public static String getFormatPrice(float price) {
        return String.format("%.2f", price);
    }

    public static String getFormatDiscount(double price) {
        return String.format("%.1f", price);
    }


    public static String urlEncode(String originString) {
        String encode = "";
        try {
            encode = URLEncoder.encode(originString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encode;
    }

    /**
     * url 解码
     */
    public static String urlDecode(String originString) {
        String decode = null;
        try {
            decode = URLDecoder.decode(originString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return decode;
    }

    /**
     * 获取 xxx.xx 格式的价格
     */
    public static String getFormatPrice(String input) {
        if (TextUtils.isEmpty(input)) {
            return "";
        }
        if (!isNumber(input)) {
            return input;
        }
        return getFormatPrice(Double.valueOf(input));
    }
    /**
     * 获取 xxx 格式的价格
     */
    public static String getFormatPriceInt(String input) {
        try {
            return input.substring(0, input.indexOf("."));
        }catch (Exception e)
        {
            return input;
        }
    }
    /**
     * 判断是否是数字，包括小数、负数
     */
    public static boolean isNumber(String input) {
        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
        return pattern.matcher(input).matches();
    }

    public static boolean isPhoneNumber(String input) {
        Pattern pattern = Pattern.compile("^1[34578][0-9]{9}$");
        return pattern.matcher(input).matches();
    }

    public static String[] getBrokenSimilarProductName(TextView tv, String text, float maxWidth) {
        if (TextUtils.isEmpty(text)) return null;

        int firstCount = tv.getPaint().breakText(text, true, maxWidth, null);
        String firstLineText = text.substring(0, firstCount);
        String secondLineText = "";
        if (firstCount < text.length()) {
            int sendCount = tv.getPaint().breakText(text.substring(firstCount), true, maxWidth, null);
            if (sendCount >= 5) {
                secondLineText = text.substring(firstCount, firstCount + sendCount - 3);
            } else {
                secondLineText = text.substring(firstCount, firstCount + sendCount);
            }
        }
        if (!TextUtils.isEmpty(secondLineText)) {
            secondLineText = secondLineText + "...";
        }

        return new String[]{firstLineText, secondLineText};
    }

    public static String getSaleNum(int num) {
        if (num >= 10000) {
            return String.format("%.1f万", num / 10000f);
        }
        return String.valueOf(num);
    }

    public static String getFormatDate(long time){
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date(time);
        return format1.format(date);
    }
}
